﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DataAccessLayer.DataInterfaces;
using MyCommon;

namespace DataAccessLayer
{
    public class LichSuBHDAO : ILichSuBH
    {
        private static QLBaoHanh dataContext;
        private IRepository<LichSuBH> repository;
        public LichSuBHDAO()
        {
            dataContext = new QLBaoHanh();
            repository = new RepositoryDAO<LichSuBH>(dataContext);
        }

        public IEnumerable<LichSuBH> GetAllLichSuBH()
        {
            //return repository.GetAll();
            return repository.GetQuery().OrderBy(aobj => aobj.Id);
        }
        public LichSuBH GetLichSuBHById(int Id)
        {
            return repository.Find(obj => obj.Id == Id).Single();
        }
        public LichSuBH GetLastRecord()
        {
            return dataContext.LichSuBHs.OrderByDescending(obj => obj.Id).FirstOrDefault();
        }

        public bool UpdateLichSuBH(LichSuBH inLichSuBH)
        {
            if (inLichSuBH == null) return false;
            LichSuBH upLichSuBH = repository.First(sobj => sobj.Id == inLichSuBH.Id);
            if (upLichSuBH == null) return false;
            upLichSuBH = inLichSuBH;
            return repository.SaveChanges() > 0;
        }
        public bool InsertLichSuBH(LichSuBH inLichSuBH)
        {
            if (inLichSuBH == null) return false;
            repository.Add(inLichSuBH);
            return repository.SaveChanges() > 0;
        }

        public ReturnValue<bool> DelLichSuBH(int Id)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                LichSuBH objDel = repository.First(obj => obj.Id == Id);
                ret = DelLichSuBH(objDel);
            }
            catch (Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }


        public ReturnValue<bool> DelLichSuBH(LichSuBH inLichSuBH)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inLichSuBH == null)
            {
                ret.Msg = "Input null";
                return ret;
            }
            try
            {
                repository.Delete(inLichSuBH);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }

        public IEnumerable<LichSuBH> GetLichSuBHByPhien_ThietBiId( int inPhien_ThietBiBHId, MyCommon.LoaiLichSu inLoaiLichSu)
        {
            //return repository.GetAll();
            return repository.GetQuery().Where(o => o.Phien_ThietBiBHId == inPhien_ThietBiBHId && o.LoaiLichSu == (int)inLoaiLichSu).OrderByDescending(aobj => aobj.ThoiGian);
        }

        public IEnumerable<LichSuBH> GetLichSuInListLoaiLichSu(int inPhien_ThietBiBHId, List<int> inListLoaiLichSuId)
        {
            return dataContext.LichSuBHs.Where(
                o => inListLoaiLichSuId.Contains(o.LoaiLichSu.Value) && o.Phien_ThietBiBHId == inPhien_ThietBiBHId)
                .Select(aObj => (LichSuBH)aObj);
        }
    }
}
